<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Classroom extends Base
{

    function __construct()
    {
    }

    public function create($param)
    {
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        if (!$sid || !$name) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
            $this->res_data['Msg'] = "无此门店";
            return $this->res_data;
        }
        $data['sid'] =$sid;
        $data['name'] =$name;
        $data['status'] =1;
        $data['createtime'] =time();
        $data['updatetime'] =time();
        $cdata = Db::name('classroom')->insertGetId($data);
        if(!$cdata) {
            $this->res_data['Msg'] = '添加班级失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function edit($param)
    {
        $cid = isset($param['cid']) ? intval($param['cid']):0;
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        $status = isset($param['status']) ? trim($param['status']):1;
        if (!$sid || !$name) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
            $this->res_data['Msg'] = "无此门店";
            return $this->res_data;
        }
        $classroom= Db::name('classroom')->where([['id','=',$cid]])->find();
        if (!$classroom) {
            $this->res_data['Msg'] = "无此班级";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['status'] =$status;
        $data['updatetime'] =time();
        $cdata = Db::name('classroom')->where([['id','=',$cid]])->data($data)->update();
        if(!$cdata) {
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function delete($param)
    {
        $cid = isset($param['cid']) ? intval($param['cid']):0;
        $status = isset($param['status']) ? intval($param['status']):0;
        $classroom = Db::name('classroom')->where([['id','=',$cid]])->find();
        if (!$classroom) {
            $this->res_data['Msg'] = "无此班级";
            return $this->res_data;
        }
        $data['status'] =$status;
        $data['updatetime'] =time();
        $cdata = Db::name('classroom')->where([['id','=',$cid]])->data($data)->update();
        if(!$cdata) {
            $this->res_data['Msg'] = '更新失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
}

?>
